package noobteam.deartrainer.controller;


import noobteam.deartrainer.service.TrainerInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 高乾威
 */
@RestController
public class TrainerInfoController {
    @Resource
    private TrainerInfoService trainerInfoService;

    @RequestMapping("/trainer_signUp")
    public String save(@RequestParam Map<String,String> map){
        String msg="注册失败";
        boolean flag=trainerInfoService.doSaveTrainer(map);
        if(flag){
            msg="注册成功";
        }
        return msg;
    }

    @RequestMapping("/trainer_signIn")
    public String trainerSignIn(@RequestParam Map<String,String> map){
        String msg = "登录失败";
        String trainerPhone = map.get("trainerPhone");
        String trainerPassword = map.get("trainerPassword");
        Map<String, Object> trainerInfoByPhone = trainerInfoService.getTrainerInfoByPhone(trainerPhone);
        if(trainerInfoByPhone.get("trainerPassword").equals(trainerPassword)){
            msg = "登录成功";
        }
        return msg;
    }

    @RequestMapping("/getTrainerIdByPhone")
    public Integer getTrainerId(@RequestParam Map<String, String> phone){
        String phoneNumber = phone.get("trainerPhone");
        Map<String, Object> trainerInfoByPhone = trainerInfoService.getTrainerInfoByPhone(phoneNumber);
        Integer id = (Integer) trainerInfoByPhone.get("trainerId");
        return id;
    }


    @RequestMapping("/trainerSignUpRepeatePhoneCheck")
    public String isPhoneValid(@RequestParam Map<String,String> js){
        List<Map<String,Object>> list = trainerInfoService.getTrainerPhone();
        String msg = "OK";
        for(Map<String,Object> map : list){
            if(map.get("trainerPhone").equals(js.get("trainerPhone"))){
                return "该手机号已存在，请更改";
            }
        }
        return msg;
    }

    @RequestMapping("/getTrainerInfoByPhone")
    public Map<String, Object> findTrainerInfoByPhone(@RequestParam Map<String, String> trainerPhone){
        String phone = trainerPhone.get("trainerPhone");
        Map<String, Object> map = trainerInfoService.getTrainerInfoByPhone(phone);
        return map;
    }

    @RequestMapping("/getTrainersList")
    public List<Map<String, Object>> getTrainersList() {

        return trainerInfoService.getTrainersList();
    }

    @RequestMapping("/getTrainersListOrderByOnJobTime")
    public List<Map<String, Object>> getTrainersListOrderByOnJobTime() {
        return trainerInfoService.getTrainersListOrderByOnJobTime();
    }

    @RequestMapping("/getTrainersListOrderByExperience")
    public List<Map<String, Object>> getTrainersListOrderByExperience() {
        return trainerInfoService.getTrainersListOrderByExperience();
    }

    @RequestMapping("/getTrainersListOrderByAge")
    public List<Map<String, Object>> getTrainersListOrderByAge() {
        return trainerInfoService.getTrainersListOrderByAge();
    }

    @RequestMapping("/getTrainersListBySex")
    public List<Map<String, Object>> getTrainersListBySex(@RequestParam String trainerSex) {
        return trainerInfoService.getTrainersListBySex(trainerSex);
    }

    @RequestMapping("/editTrainerInfo")
    public String updateTrainerInfo(@RequestParam Map<String,String> map){
        String msg="修改失败";
        boolean flag = trainerInfoService.updateTrainerMsg(map);
        if(flag){
            msg="修改成功";
        }
        return msg;
    }

    @RequestMapping("/updateTrainerImage")
    public String updateTrainerImage(@RequestParam Map<String, String> trainer) {
        boolean a = trainerInfoService.updateTrainerImage(trainer);
        if (a)
        {
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @RequestMapping("/deleteTrainerByPhone")
    public String deleteTrainer(String phone){
        String msg="删除失败";
        boolean flag = trainerInfoService.deleteTrainer(phone);
        if(flag){
            msg="删除成功";
        }
        return msg;
    }
}
